<template>
   <el-dialog
    :title="pojo.id == null ? '新增员工' : '修改员工'"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form 
            :rules="rules"
            ref="pojoForm"
            label-width="100px"
            label-position="right"
            style="width: 400px;"
            :model="pojo">
                <el-form-item label="帐号" prop="username" >
                    <el-input v-model="pojo.username" ></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name" >
                    <el-input v-model="pojo.name" ></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age" >
                   <el-input v-model="pojo.age" ></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobile">
                    <el-input v-model="pojo.mobile" ></el-input>
                </el-form-item>
                <el-form-item label="薪酬" prop="salary">
                    <el-input v-model="pojo.salary" ></el-input>
                </el-form-item>
                <el-form-item label="入职时间" prop="entryDate">
                    <el-date-picker style="width: 200px" value-format="timestamp" v-model="pojo.entryDate" placeholder="选择入职时间" type="date" > </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="submitForm('pojoForm')">确 定</el-button>
            </div>
  </el-dialog>
</template>

<script>
import staffApi from "@/api/staff";
export default {
  data() {
    return {
      rules: {
            username: [
                {required: true, message: '帐号不能为空', trigger: 'blur'}
            ],
            name: [
                {required: true, message: '姓名不能为空', trigger: 'blur'}
            ]
        }
    };
  },

  props: ["dialogVisible", "pojo", "closeDialog"],

  components: {},

  methods: {
    // 提交新增数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //提交表单
          this.addOrUpdate();
        }
      });
    },

    async addOrUpdate() {
      await staffApi.add(this.pojo);
      this.$message("成功");
      //新增成功，刷新列表数据
      this.handleClose(); // 关闭窗口
    },

    handleClose() {
      this.$nextTick(() => {
        this.$refs["pojoForm"].resetFields();
        this.closeDialog();
      });
    },
  },
};
</script>

<style scoped>
</style>